layui.use(['table', 'admin', 'ax', 'form', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 管理
     */
    var Project = {
        tableId: "projectTable"
    };

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', sort: true, title: '工程id', align: "center",},
            {field: 'name', sort: true, title: '工程名称', align: "center",},
            {field: 'createUserName', sort: false, title: '添加人', align: "center",},
            {field: 'createTime', sort: true, title: '添加时间', align: "center",},
            {align: 'center', toolbar: '#tableBar', title: '操作', align: "center", minWidth: 500}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['provinceName'] = $("#provinceName").val();
        queryData['cityName'] = $("#cityName").val();
        queryData['name'] = $("#address").val();
        table.reload(Project.tableId, {where: queryData});
    };

    /**
     * 弹出添加通知
     */
    Project.openAddDlg = function () {
        var provinceName = $("#provinceName").val();
        if (provinceName == "") {
            Feng.error("请选择省!");
            return;
        }
        var cityName = $("#cityName").val();
        if (cityName == "") {
            Feng.error("请选择市!");
            return;
        }
        var address = $("#address").val();
        if (address == "") {
            Feng.error("请输入店名!");
            return;
        }

        var ajax1 = new $ax(Feng.ctxPath + "/project/addItem", function (data) {
            if (data == 1) {
                Feng.error("添加失败,店名格式有误!");
                return;
            }
            Feng.success("添加成功!");
            table.reload(Project.tableId);
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax1.set("provinceName", $("#provinceName").find("option:selected").text());
        ajax1.set("provinceId", $("#provinceName").val());
        ajax1.set("cityId", $("#cityName").val());
        ajax1.set("cityName", $("#cityName").find("option:selected").text());
        ajax1.set('name', $("#address").val());
        ajax1.start();
    };

    /**
     * 导出excel按钮
     */
    Project.exportExcel = function () {
        var checkRows = table.checkStatus(Project.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击填表（商务）
     */
    Project.things = function (data) {
        window.location.href = Feng.ctxPath + '/project/things?id=' + data.id;
    };

    /**
     * 点击填表（工程）
     */
    Project.projectTerm = function (data) {
        window.location.href = Feng.ctxPath + '/project/projectTerm?id=' + data.id;
    };

    /**
     * 点击跳转督导完成任务页面（需完成任务）
     */
    Project.steerTask = function (data) {
        window.location.href = Feng.ctxPath + '/task/steerTask?id=' + data.id;
    };

    /**
     * 点击跳转工程完成任务页面（工程需完成任务）
     */
    Project.projectTask = function (data) {
        window.location.href = Feng.ctxPath + '/task/projectTask?id/**/=' + data.id;
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Project.tableId,
        url: Feng.ctxPath + '/project/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Project.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Project.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Project.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Project.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'manager') {
            $("#YDName").html("营运经理");
            $("#appointMsg").html("");
            $.ajax({
                type: "get",
                url: "/project/selectYyjlList",
                async: false,
                success: function (data) {
                    for (let i = 0; i < data.length; i++) {
                        $("#appointMsg").append('<option value="' + data[i].yyjl + '">' + data[i].yyjl + '</option>');
                    }
                    layer.open({
                        area: ['500px', '500px'],
                        title: '指定营运经理',
                        btn: ['确定', '取消'],
                        btnAlign: 'c',
                        type: 1,
                        async: false,
                        content: $("#hideYX").html()
                    });
                }
            });
        } else if (layEvent === 'supervision') {
            $("#YDName").html("督导");
            $("#appointMsg").html("");
            $.ajax({
                type: "get",
                url: "/project/selectDdList",
                async: false,
                success: function (data) {
                    for (let i = 0; i < data.length; i++) {
                        $("#appointMsg").append('<option value="' + data[i].dd + '">' + data[i].dd + '</option>');
                    }
                    layer.open({
                        area: ['500px', '500px'],
                        title: '指定督导',
                        btn: ['确定', '取消'],
                        btnAlign: 'c',
                        type: 1,
                        content: $("#hideYX").html()
                    });
                }
            });

        } else if (layEvent === 'things') {
            Project.things(data);
        } else if (layEvent === 'projectTerm') {
            Project.projectTerm(data);
        } else if (layEvent === 'internetStore') {
            Project.internetStore(data);
        } else if (layEvent === 'surfMeal') {
            Project.surfMeal(data);
        } else if (layEvent === 'queuing') {
            Project.queuing(data);
        } else if (layEvent === 'premises') {
            Project.premises(data);
        } else if (layEvent === 'steerTask') {
            Project.steerTask(data);
        } else if (layEvent === 'projectTask') {
            Project.projectTask(data);
        }
    })
    ;

//表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/project/addItem", function (data) {
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/project'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message);
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/area/getAreaByType");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, data) {
        $("#provinceName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
    });
    form.render('select');
    form.on('select(provinceName)', function (data) {
        var provinceId = data.value;
        if (provinceId == "") {
            $("#cityName").html("");
            $("#cityName").append("<option value=''>请选择市级</option>");
            form.render('select');
        } else {
            var qyAjax = new $ax("/area/getAreaByPid");
            qyAjax.set("pid", provinceId);
            var qyResult = qyAjax.start();
            $("#cityName").html("");
            $.each(qyResult, function (index, data) {
                $("#cityName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
            });
            form.render('select');
        }
    });
});